package com.elvt.bolg.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.elvt.bolg.mapper.UserMapper;
import com.elvt.bolg.pojo.User;
import com.elvt.bolg.service.UserService;
import com.elvt.bolg.util.SqlSessionFactoryUtils;
import com.elvt.bolg.vo.ReturnInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.sql.ResultSet;


public class UserServiceImpl implements UserService {
    //1.创建sqlSessionFactory工厂对象
    SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public ReturnInfo queryUserByusername(String username, String password, String code, HttpServletRequest request) {
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return new ReturnInfo(false, username, "输入的信息不能为空！");
        }


        String loginCodeRes = (String) request.getSession().getAttribute("loginCodeRes");
        if (!code.equalsIgnoreCase(loginCodeRes)) {
            request.getSession().removeAttribute("loginCodeRes");
            return new ReturnInfo(false, username, "输入的验证码错误！");
        }
            SqlSession sqlSession = factory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user = mapper.queryUserByusername(username);
            sqlSession.close();
            if (user == null) {
                return new ReturnInfo(false, "该用户不存在！");
            }
            //采用hutool的工具对密码进行一个md5的加密
            password = DigestUtil.md5Hex(password);
            //判断加密后的是否与数据库中的一致
            if (!user.getPassword().equals(password)) {
                return new ReturnInfo(false, username, "密码不正确！");
            }
            return new ReturnInfo(true, user);

    }

    @Override
    public Boolean checkNick(String nick, Integer userid) {
        if (StrUtil.isBlank(nick)){
            return false;
        }
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryUserByNickAndUserID(nick,userid);
        sqlSession.close();
        if (user==null){
            return false;
        }


        return true;
    }

    @Override
    public ReturnInfo updateUser(HttpServletRequest req) {

        String nick = req.getParameter("nick");
        String mood = req.getParameter("mood");
        String img = req.getParameter("img");
        System.out.println("img___"+img);
        System.out.println("mood__"+nick);
        System.out.println("mood__"+mood);
        if (StrUtil.isBlank(nick)){
           return new ReturnInfo(false,"昵称不能为空");

        }
        User user = (User) req.getSession().getAttribute("user");
        //设置修改的昵称和头衔
        user.setNick(nick);
        user.setMood(mood);
        try {
            Part part = req.getPart("img");
            String header = part.getHeader("Content-Disposition");
            //获取具体请求头对应的值
            String str = header.substring(header.lastIndexOf("=") + 2);
            //获取上传文件名
            String fileName = str.substring(0, str.length() - 1);
            //判断文件是否为空
            if (!StrUtil.isBlank(fileName)){
                //如果用户上传了头衔，则更新用户对象中的头衔
                user.setHead(fileName);
                //获取文件存放路径
                String filePath=req.getServletContext().getRealPath("/WEB-INF/upload/");
                //上传文件指定目录
                part.write(filePath+"/"+fileName);

            }
        }catch (Exception e){
        e.printStackTrace();
        }
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int row  = mapper.updateUser(user);

        sqlSession.close();
        if (row>0){
            return new ReturnInfo(true,"修改成功！");
        }
        return new ReturnInfo(false,"修改失败！");
    }
}
